package com.lagou.worker.controller;

import com.lagou.worker.model.LagouToken;
import com.lagou.worker.service.CodeService;
import com.lagou.worker.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author xsq
 * @Date 2020/7/30 17:12
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private CodeService codeService;
    @Autowired
    private UserService userService;

    @PostMapping(value = "/register/{email}/{password}/{code}")
    public ResponseEntity register(@PathVariable(value = "email") String email,
                                   @PathVariable(value = "password") String password,
                                   @PathVariable(value = "code") String code, HttpServletResponse response) {
        ResponseEntity<Integer> validate = codeService.validate(email, code);
        if (validate.getBody() == 0) {
            //注册
            userService.register(email, password);
            return ResponseEntity.ok(true);
        } else if (validate.getBody() == 1) {
            return ResponseEntity.badRequest().body("验证码不正确");
        } else if (validate.getBody() == 2) {
            return ResponseEntity.badRequest().body("验证码已过期");
        } else {
            return ResponseEntity.badRequest().body("验证码不存在");
        }
    }

    @GetMapping(value = "/isRegistered/{email}")
    public ResponseEntity isRegistered(@PathVariable("email") String email) {
        return ResponseEntity.ok(userService.isRegister(email));
    }

    @GetMapping(value = "/info")
    public ResponseEntity register(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if ("token".equals(cookie.getName())) {
                String token = cookie.getValue();
                LagouToken emailByToken = userService.getEmailByToken(token);
                return ResponseEntity.ok(emailByToken.getEmail());
            }
        }
        return ResponseEntity.badRequest().body("未登录");
    }

    /**
     * 用户登录
     *
     * @param email
     * @param password
     * @param response
     * @return
     */
    @PostMapping(value = "/login/{email}/{password}")
    public ResponseEntity register(@PathVariable(value = "email") String email,
                                   @PathVariable(value = "password") String password,
                                   HttpServletResponse response) {
        try {
            String userEmail = userService.login(email, password);
            //生成token 存入数据库
            String token = UUID.randomUUID().toString();
            userService.saveToken(email, token);
            //token保存进cookie
            Cookie cookie = new Cookie("token", token);
            cookie.setPath("/");
            response.addCookie(cookie);
            return ResponseEntity.ok(userEmail);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
}
